package sys

import (
	"fmt"
	"github.com/gin-gonic/gin"
	nhttp "net/http"
	"wzgames/internal/admin/adc"
	"wzgames/internal/admin/api/mid"
	"wzgames/internal/admin/model"
	"wzgames/internal/response"
	"wzgames/pkg/server/http"
	"wzgames/pkg/server/http/types"
)

func RegisterRoute(pGroup *gin.RouterGroup) {
	bizGroup := pGroup.Group("sys")
	registerCurrRoute(bizGroup)
	registerGameCurrRoute(bizGroup)
	registerLangRoute(bizGroup)
	registerGameRoute(bizGroup)
	http.MethodMapper(bizGroup, types.GET)(adminInfoFn())
	http.MethodMapper(bizGroup, types.GET)(logoutFn())
}

var adminInfoFn = func() (string, func(c *gin.Context)) {
	uri := fmt.Sprintf("/admin")
	return uri, func(c *gin.Context) {
		adminId := mid.AdminId(c)
		adm, code := adc.PP().GetAdmin(adminId)
		if code != 0 {
			c.JSON(nhttp.StatusOK, model.Response{Code: code})
			return
		}

		c.JSON(nhttp.StatusOK, model.Response{
			Code: code,
			Data: gin.H{
				"userId":   adm.AdminID,
				"username": adm.AdminName,
				"realName": adm.NickName,
				"avatar":   adm.Avatar,
			},
		})
		return
	}
}

var logoutFn = func() (string, func(c *gin.Context)) {
	uri := fmt.Sprintf("/logout")
	return uri, func(c *gin.Context) {
		adminId := mid.AdminId(c)
		adm := adc.GetReceiver().GetAdmin(adminId)
		if adm == nil {
			c.JSON(nhttp.StatusOK, model.Response{Code: response.ErrUnAuthorize})
			return
		}
		adm.Logout()

		c.JSON(nhttp.StatusOK, model.Response{Code: 0})
		return
	}
}
